/*
 *  enum Result<T, E> {
 *      Ok(T),
 *      Err(E),
 *  }
 */
pub fn use_error(){
    use_panic_error();
    use_result_error();
    error_passby();
}
fn use_panic_error(){
    //panic!("error occurred");// RUST_BACKTRACE=1
}

use std::fs::File;
fn use_result_error(){
    let f = File::open("hello.txt"); // Result<File, String>
    match f {
        Ok(_) => { //no move
            println!("file open success!");
        },
        Err(_) => { // no move
            println!("file open failed!");
        }
    }
    
    if let Ok(_) = f { //no move
        println!("file open success!");
    } else {
        println!("file open failed");
    }
    
    result_to_panic();
}

fn result_to_panic(){
    //let _f1 = File::open("hello.txt").unwrap(); // panic!()
    //let _f2 = File::open("helloworld.txt").expect("Failed to open"); // panic!(string)
}


fn error_passby(){
    fn internal_error(i: i32) -> Result<i32, bool> {
        if i >= 0 { Ok(i) }
        else {Err(false) }
    }

    /*fn passby_error_v1(i: i32) -> Result<i32, bool> {
        let t = internal_error(i);
        return match t {
            Ok(i) => Ok(i),
            Err(b) => Err(b)
        };
    }
    let r = passby_error_v1(-1);
    */

    fn passby_error(i: i32) -> Result<i32, bool> {
        internal_error(i)?;
        Ok(i)
    }
    let r = passby_error(-1);

    if let Ok(i) = r {
        println!("Ok : {}", i);
    } else {
        println!("Failed: got error from internal_error() & pass by passby_error()");
    }
}

